package com.srwl.mytx.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;

import androidx.fragment.app.FragmentTransaction;

import com.srwl.easeui.widget.MyTitleBar;
import com.srwl.mytx.R;
import com.srwl.mytx.fragment.DonationRecordFragment;
import com.srwl.mytx.fragment.ReceivedDonationRecordFragment;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.ClickUtil;

public class DonationRecordActivity extends BaseActivity {
    private boolean showReceivedDonation = true;
    private ReceivedDonationRecordFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_donation_record);
        final ProgressBar progressBar = findViewById(R.id.progress_bar);
        MyTitleBar titleBar = findViewById(R.id.title_bar);
        titleBar.setRightLayoutVisibility(View.VISIBLE);
        //titleBar.titleLayout.setVisibility(View.VISIBLE);

        DonationRecordFragment donationRecordFragment = DonationRecordFragment.newInstance(UserProfileManager.getInstance().getLoginUserInfo().getuId());
        donationRecordFragment.setListener(new DonationRecordFragment.InteractionListener() {
            @Override
            public void showProgressBar(boolean isShowProgressBar) {
                if (isShowProgressBar && !progressBar.isShown()) {
                    progressBar.setVisibility(View.VISIBLE);
                } else if (!isShowProgressBar && progressBar.isShown()) {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
        final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fl_container, donationRecordFragment).show(donationRecordFragment).commit();

        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isFastClick()) return;
                final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                if (showReceivedDonation) {
                    titleBar.setTitle("收到捐赠");
                    if (fragment == null) {
                        fragment = ReceivedDonationRecordFragment.newInstance();
                    }
                    fragmentTransaction.hide(donationRecordFragment);
                    if (!fragment.isAdded()) {
                        fragmentTransaction.add(R.id.fl_container, fragment);
                    }
                    fragmentTransaction.show(fragment).commit();
                    showReceivedDonation = false;
                } else {
                    titleBar.setTitle("发出捐赠");
                    if (!donationRecordFragment.isAdded()) {
                        fragmentTransaction.add(R.id.fl_container, donationRecordFragment);
                    }
                    if (fragment != null) {
                        fragmentTransaction.hide(fragment);
                    }
                    fragmentTransaction.show(donationRecordFragment).commit();
                    showReceivedDonation = true;
                }
            }
        });
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    //    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//    }
}
